home *** CD-ROM | disk | FTP | other *** search
- package symantec.itools.db.awt;
-
- import java.awt.Color;
- import java.awt.Event;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.image.ImageObserver;
-
- public class ButtonCell implements TableCell, ImageObserver {
- Grid view;
- DataSource dataSource;
- Coordinate coords;
- boolean pressed;
- boolean drawUp = true;
- boolean defaultFlag;
- int type;
- static final int PADSIDES = 5;
-
- ButtonCell() {
- }
-
- public ButtonCell(Grid v, DataSource ds) {
- this.view = v;
- this.dataSource = ds;
- }
-
- public ButtonCell(int r, int c) {
- this.coords = new Coordinate(r - 1, c - 1);
- }
-
- public int type() {
- return this.type;
- }
-
- public int type(int t) {
- if (t <= 3 && t >= 0) {
- return this.type = t;
- } else {
- throw new IllegalArgumentException("Invalid cell type");
- }
- }
-
- public TableCell cloneCell() {
- ButtonCell bc = new ButtonCell(this.view, this.dataSource);
- if (this.coords != null) {
- bc.coords = new Coordinate(this.coords);
- }
-
- bc.type = this.type;
- bc.defaultFlag = this.defaultFlag;
- return bc;
- }
-
- public void setGrid(Grid v, DataSource ds) {
- this.view = v;
- this.dataSource = ds;
- }
-
- public void setDefaultFlag() {
- this.defaultFlag = true;
- }
-
- public void reset() {
- this.pressed = false;
- this.drawUp = true;
- }
-
- public void setCoordinates(Coordinate p) {
- this.coords = p;
- }
-
- public Coordinate getCoordinates() {
- return this.coords;
- }
-
- public int row() {
- return this.coords.row;
- }
-
- public void setRow(int r) {
- this.coords.row = r;
- }
-
- public int col() {
- return this.coords.col;
- }
-
- public void setCol(int c) {
- this.coords.col = c;
- }
-
- boolean inside(int x, int y) {
- Rectangle r = this.view.getCellBounds(this);
- return x > 0 && y > 0 && x < r.width && y < r.height;
- }
-
- public boolean mouseEvent(Event e) {
- if (e.id == 501) {
- this.view.setCapture(this);
- this.pressed = true;
- this.drawUp = false;
- this.view.generateEvent(e, 50, this);
- this.view.redrawCell(this);
- } else if (e.id == 506) {
- if (this.inside(e.x, e.y) && this.drawUp) {
- this.drawUp = false;
- this.view.generateEvent(e, 52, this);
- this.view.redrawCell(this);
- } else if (!this.inside(e.x, e.y) && !this.drawUp) {
- this.drawUp = true;
- this.view.generateEvent(e, 53, this);
- this.view.redrawCell(this);
- }
- } else if (e.id == 502) {
- if (this.pressed && !this.drawUp) {
- this.view.generateEvent(e, 51, this);
- }
-
- this.pressed = false;
- this.drawUp = true;
- this.view.redrawCell(this);
- this.view.lostCapture();
- }
-
- return true;
- }
-
- public Data getData() throws DataNotAvailable {
- return this.dataSource.getData(this.coords);
- }
-
- String chopString(String text, FontMetrics fm, Rectangle r) {
- int w = r.width;
- int i = 1;
- if (text.length() == 0) {
- return text;
- } else {
- String t;
- do {
- t = text.substring(0, i);
- } while(fm.stringWidth(t) < w - 10 && i++ < text.length());
-
- return text.substring(0, i - 1);
- }
- }
-
- public void drawCell(Graphics g, CellHints hints) {
- Data data;
- try {
- data = this.getData();
- } catch (DataNotAvailable var11) {
- data = new ImageStringData(this.dataSource, "");
- }
-
- Rectangle r = hints.bounds();
- FontMetrics fm = g.getFontMetrics();
- fm.getAscent();
- String text = this.chopString(data.toString(), fm, r);
- int sw = fm.stringWidth(text);
- int imageOffset = 0;
- g.getColor();
- int origX = r.x;
- this.drawButton(g, r);
- Image im = data.toImage();
- switch (hints.alignment()) {
- case 0:
- if (im != null) {
- imageOffset = im.getWidth(this) + 5;
- }
-
- if (imageOffset + sw + 2 + 5 <= r.width && im != null) {
- r.x += 5;
- r.y += 3;
- g.drawImage(im, r.x, r.y, this);
- r.y -= 3;
- } else {
- imageOffset = 5;
- }
-
- r.x = origX + imageOffset + 2;
- break;
- case 1:
- r.x = origX + (r.width - sw) / 2;
- break;
- case 2:
- if (im != null) {
- imageOffset = im.getWidth(this) + 5;
- }
-
- if (sw + imageOffset + 2 + 5 <= r.width && im != null) {
- r.x = r.x + r.width - sw - 5 - imageOffset - 2;
- r.y += 3;
- g.drawImage(im, r.x, r.y, this);
- r.y -= 3;
- }
-
- r.x = origX + r.width - sw - 5;
- }
-
- g.setColor(hints.foreground());
- g.drawString(text, r.x, r.y + fm.getAscent() + 2);
- }
-
- void drawButton(Graphics g, Rectangle r) {
- Color bg = Color.lightGray;
- g.setColor(bg);
- g.fillRect(r.x, r.y, r.width - 1, r.height);
- g.setColor(Color.gray);
- g.drawRect(r.x + 1, r.y + 1, r.width - 3, r.height - 3);
- g.setColor(Color.black);
- g.drawRect(r.x, r.y, r.width - 1, r.height - 1);
- if (this.drawUp) {
- g.setColor(Color.white);
- g.drawLine(r.x + 1, r.y + 1, r.x + r.width - 2, r.y + 1);
- g.drawLine(r.x + 1, r.y + 1, r.x + 1, r.y + r.height - 2);
- } else {
- g.setColor(Color.white);
- g.drawLine(r.x + 1, r.y + r.height - 2, r.x + r.width - 2, r.y + r.height - 2);
- g.drawLine(r.x + r.width - 2, r.y + 1, r.x + r.width - 2, r.y + r.height - 3);
- }
- }
-
- public boolean isCellTypeEditable() {
- return false;
- }
-
- public boolean actionEvent(Event e) {
- return true;
- }
-
- public boolean keyEvent(Event e) {
- return true;
- }
-
- public boolean canLoseFocus() {
- return true;
- }
-
- public boolean focusEvent(Event e) {
- if (e.id == 1004) {
- this.view.setCapture(this);
- this.view.generateEvent(e, 55, this);
- } else {
- this.view.lostCapture();
- this.view.generateEvent(e, 56, this);
- }
-
- return true;
- }
-
- public boolean loseFocusOnArrow() {
- return false;
- }
-
- public void activateCursor() {
- }
-
- public void deactivateCursor() {
- }
-
- public String stats() {
- return "";
- }
-
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
- if ((flags & 192) != 0) {
- return false;
- } else if ((flags & 32) != 0) {
- this.view.redraw(true);
- return false;
- } else {
- return true;
- }
- }
- }
-